package com.longe.nio.channel;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelTest001 {

    public static void main(String[] args) throws Exception {
        String message = "hello nio channel";
        //定义一个文件输出流
        FileOutputStream fos = new FileOutputStream("demo1.txt");
        //获取channel
        FileChannel fileChannel = fos.getChannel();

        //定义一个Buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(128);
        //将message放入buffer
        byteBuffer.put(message.getBytes());

        //翻转
        byteBuffer.flip();

        //将buffer中的数据写入通道
        fileChannel.write(byteBuffer);

        //关闭通道和流
        fileChannel.close();
        fos.close();
    }
}
